<template>
	<div style="height: 100%">
		<div>
			<div style="height: 460px">
				<zh-tree
					ref="treeRef"
					show-checkbox
					:default-expanded-keys="state.expandedKeys"
					:default-checked-keys="state.checkedKeys"
					:autoExpandParent="state.autoExpandParent"
					:data="state.flowNodeList"
					:props="{ children: 'children', label: 'title' }"
					node-key="key"
				>
				</zh-tree>
			</div>
		</div>
	</div>
</template>

<script lang="ts" setup name="FlowNodeTree">
import { ElTree } from 'element-plus';

//------------------接收传入参数(组件)-------------------
const props = defineProps({
	userId: {
		type: String,
		default: null,
		required: false,
	},
	roleId: {
		type: String,
		default: null,
		required: false,
	},
	modelValue: Boolean,
});
const emit = defineEmits(['update:modelValue']);

//------------------定义子组件---------------------------
const treeRef = ref<InstanceType<typeof ElTree>>();

//------------------内部变量定义-------------------------
const state = reactive({
	description: '流程节点管理',
	// 功能树
	expandedKeys: [],
	// selectedKeys: [],
	searchValue: '',
	autoExpandParent: true,
	flowNodeList: [] as any,
	checkedKeys: [],
});

//------------------定义调用方法-------------------------
function loopExpandKey(data, keys, expandKeys) {
	let flag = false;
	data.forEach((item) => {
		let itemFlag = false;
		const orgId = item.key;
		keys.some((item) => {
			if (orgId === item) {
				itemFlag = true;
				return true;
			}
		});
		if (item.children) {
			const childFlag = loopExpandKey(item.children, keys, expandKeys);
			if (childFlag) {
				itemFlag = true;
			}
		}
		if (itemFlag) {
			flag = true;
			expandKeys.push(item.key);
		}
	});
	return flag;
}
function findFlowNodeTreeList() {
	const params = {
		searchText: state.searchValue,
		roleId: props.roleId,
		userId: props.userId,
	};
	setTimeout(() => {
    $api.common.send
        .post('flowLongDeploy015', params)
        .then((res) => {
          const data = [...res.result.treeList];
          if (data) {
            state.flowNodeList = data;
            state.checkedKeys = [];
            const expandedKeys = res.result.expandedKeys;
            const checkedKeys = res.result.checkedKeys;
            if (checkedKeys && checkedKeys.length > 0) {
              // 设置展开节点
              const expandedFlowNodeKeys = [];
              loopExpandKey(data, checkedKeys, expandedFlowNodeKeys);
              state.checkedKeys = checkedKeys;
              state.expandedKeys = expandedFlowNodeKeys;
            } else {
              state.expandedKeys = expandedKeys;
            }
            state.autoExpandParent = true;
          }
        })
        .catch(() => {});
	}, 50);
}

function getCheckedKeys() {
	return treeRef.value!.getCheckedKeys(false);
}

function saveFlowNode(type: string) {
	if (type === 'user') {
		if (props.userId) {
			const checkedkeys: any = getCheckedKeys();
			const params = {} as any;
			if (Array.isArray(checkedkeys)) {
				params.objectIds = checkedkeys;
			} else {
				params.objectIds = checkedkeys.checked;
			}
			params.userId = props.userId;
			setTimeout(async () => {
				return await $api.common.send
            .post('flowLongDeploy016', params)
            .then((res) => {
              $messageBox.success('授权成功', '提示');
              emit('update:modelValue', false);
            })
            .catch(() => {});
			}, 10);
		} else {
			$messageBox.warning('请先选择需要操作的用户', '提示');
		}
	} else if (type === 'role') {
		if (props.roleId) {
			const checkedkeys: any = getCheckedKeys();
			const params = {
				objectIds: [] as any[],
				roleId: null as any,
			};
			if (Array.isArray(checkedkeys)) {
				params.objectIds = checkedkeys;
			} else {
				params.objectIds = checkedkeys.checked;
			}
			params.roleId = props.roleId;
			setTimeout(async () => {
        return await $api.common.send
            .post('flowLongDeploy016', params)
            .then((res) => {
              $messageBox.success('授权成功', '提示');
              emit('update:modelValue', false);
            })
            .catch(() => {
            });
      }, 10);
		} else {
			$messageBox.warning('请先选择需要操作的角色', '提示');
		}
	}
}

//------------------初始化------------------------------
onMounted(() => {
  nextTick(()=>{
    findFlowNodeTreeList();
  })
});

//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({
	getCheckedKeys,
	saveFlowNode,
});
</script>
<style></style>
